﻿using System;
using System.Collections.Generic;
using System.Text;
using DCRF.Common.Connection;

namespace DCRF.Common.Core
{
    public class ProxyConnector : IGlobalConnector
    {
        private ProxyBlockWeb parentWeb = null;
        private ProxyBlock parentBlock = null;
        private string key = null;

        public ProxyConnector(string k, ProxyBlockWeb parentW, ProxyBlock parentB)
        {
            key = k;
            parentWeb = parentW;
            parentBlock = parentB;
        }

        public string AttachEndPoint(Guid blockHandle, string serviceName)
        {
            return AttachEndPoint(blockHandle, serviceName, null);
        }

        public string AttachEndPoint(Guid blockHandle, string serviceName, object[] predefinedValues)
        {
            if (parentBlock != null)
            {
                return (string)parentWeb.CallConnectorMethod(parentBlock.Id, key, "AttachEndPointG", blockHandle, serviceName, predefinedValues);
            }
            else
            {
                return (string)parentWeb.CallConnectorMethod(Guid.Empty, key, "AttachEndPointG", blockHandle, serviceName, predefinedValues);
            }
        }

        public string AttachEndPoint(IConnectorSubject target, string serviceName)
        {
            throw new NotImplementedException("Interface connector attachment is not supported");
        }

        public string AttachEndPoint(object value)
        {
            if (parentBlock != null)
            {
                return (string)parentWeb.CallConnectorMethod(parentBlock.Id, key, "AttachEndPointV", value);
            }
            else
            {
                return (string)parentWeb.CallConnectorMethod(Guid.Empty, key, "AttachEndPointV", value);
            }
        }

        public string ChainEndPoint(Guid blockHandle, string chainConnectorKey)
        {
            if (parentBlock != null)
            {
                return (string)parentWeb.CallConnectorMethod(parentBlock.Id, key, "ChainEndPoint", blockHandle, chainConnectorKey);
            }
            else
            {
                return (string)parentWeb.CallConnectorMethod(Guid.Empty, key, "ChainEndPoint", blockHandle, chainConnectorKey);
            }
        }

        public void DetachEndPoint(string epkey)
        {
            if (parentBlock != null)
            {
                parentWeb.CallConnectorMethod(parentBlock.Id, key, "DetachEndPoint", epkey);
            }
            else
            {
                parentWeb.CallConnectorMethod(Guid.Empty, key, "DetachEndPoint", epkey);
            }
        }

        public List<object> ProcessRequestAll(params object[] args)
        {
            if (parentBlock != null)
            {
                throw new Exception("You cannot call an internal connector of a block");
            }
            else
            {
                return (List<object>)parentWeb.CallConnectorMethod(Guid.Empty, key, "ProcessRequestAll", args);
            }
        }

        public object ProcessRequest(params object[] args)
        {
            List<object> result = ProcessRequestAll(args);

            if (result.Count == 0) return null;

            return result[0];
        }

        public void Raise(params object[] args)
        {
            ProcessRequestAll(args);
        }
    }
}
